﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FernUniHagen.CompilerExtension.DataStructures;
using Roslyn.Compilers.Common;
using Roslyn.Compilers.CSharp;

namespace FernUniHagen.CompilerExtension.Converter
{
    public abstract class NodeConverterFactory
    {
        private CommonSyntaxNode _node;
        //private MethodDeclarationSyntax _methodDeclaration;


        protected CommonSyntaxNode Node
        {
            get { return _node; }
            set { _node = value; }
        }

       

        //protected MethodDeclarationSyntax MethodDeclaration
        //{
        //    get { return _methodDeclaration; }
        //    set { _methodDeclaration = value; }
        //}


        public static NodeConverterFactory GetConverter(ISemanticModel semanticModel, CommonSyntaxNode node, List<SyntaxNodeInfo> nodeInfos)
        {

            NodeConverterFactory nodeFactory = null;

            if (node is FieldDeclarationSyntax)
                nodeFactory = new FieldDeclarationConverter();
            else if (node is PropertyDeclarationSyntax)
                nodeFactory = new PropertyDeclarationConverter();
            else if (node is ParameterSyntax)
                nodeFactory = new ParameterConverter();
            else if (node is MethodDeclarationSyntax)
                nodeFactory = new MethodDeclarationConverter();
            else if (node is InvocationExpressionSyntax)
                return null;
            else if (node is BlockSyntax)
                nodeFactory = new BlockSyntaxConverter(semanticModel, nodeInfos);
            else if (node is ClassDeclarationSyntax)
                nodeFactory = new ClassDeclarationConverter();

            nodeFactory.Node = node;
            return nodeFactory;
        }


        public CommonSyntaxNode TryToConvert()
        {
            CommonSyntaxNode node = Node;
            try
            {
               node = Convert();
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("Could not convert the node: {0}\nWith {1} Converter", Node.ToFullString(), this.ToString()));
            }
            return node;

        }
       
        
        public abstract CommonSyntaxNode Convert();




    }
}